// Copyright Wilfried Woivre - http://blog.woivre.fr

using System.Collections.Generic;
using System.Data.Services.Providers;

namespace Demo.TableStorageDataServices.Web.Services
{
	public class MetadataProvider : IDataServiceMetadataProvider
	{
		private readonly Dictionary<string, ResourceType> _resourceTypes = new Dictionary<string, ResourceType>();
		private readonly Dictionary<string, ResourceSet> _resourceSets = new Dictionary<string, ResourceSet>();
		private readonly List<ResourceAssociationSet> _associationSets = new List<ResourceAssociationSet>();
		
		public void AddResourceType(ResourceType type)
		{
			type.SetReadOnly();
			_resourceTypes.Add(type.FullName, type);
		}
		
		public void AddResourceSet(ResourceSet set)
		{
			set.SetReadOnly();
			_resourceSets.Add(set.Name, set);
		}
		
        public void AddAssociationSet(ResourceAssociationSet resourceAssociationSet)
        {
            _associationSets.Add(resourceAssociationSet);
        }

        public bool TryResolveResourceSet(string name, out ResourceSet resourceSet)
        {
            return _resourceSets.TryGetValue(name, out resourceSet); 
        }

        public ResourceAssociationSet GetResourceAssociationSet(ResourceSet resourceSet, ResourceType resourceType, ResourceProperty resourceProperty)
        {
            return resourceProperty.CustomState as ResourceAssociationSet;
        }

        public bool TryResolveResourceType(string name, out ResourceType resourceType)
        {
            return _resourceTypes.TryGetValue(name, out resourceType);
        }

        public IEnumerable<ResourceType> GetDerivedTypes(ResourceType resourceType)
        {
            yield break;
        }

        public bool HasDerivedTypes(ResourceType resourceType)
        {
            return false;
        }

        public bool TryResolveServiceOperation(string name, out ServiceOperation serviceOperation)
        {
            serviceOperation = null;
            return false;
        }

        public string ContainerNamespace
        {
            get { return "Namespace"; }
        }

        public string ContainerName
        {
            get { return "Container"; }
        }

        public IEnumerable<ResourceSet> ResourceSets
        {
            get { return _resourceSets.Values; }
        }

        public IEnumerable<ResourceType> Types
        {
            get { return _resourceTypes.Values; }
        }

        public IEnumerable<ServiceOperation> ServiceOperations
        {
            get { yield break; }
        }
	}
}

